﻿using System;
using System.Collections.Generic;
using System.Text;
using PhatPhucLoc.model;
using PhatPhucLoc.util;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using PhatPhucLoc.common;

namespace PhatPhucLoc.dao
{
    class ThanhVienDAO
    {
        /***/
        // hàm lấy thông tin user login
        /***/

        public static ThanhVienModel GetThanhVien(String tenDangNhap, String matKhau)
        {
            ThanhVienModel model = null;
                SqlParameter[] param = new SqlParameter[2];
                int i = 0;

                param[i] = new SqlParameter("TenDangNhap", SqlDbType.NVarChar);
                param[i++].Value = tenDangNhap;
                param[i] = new SqlParameter("MatKhau", SqlDbType.NVarChar);
                param[i++].Value = matKhau;
                DataTable kq = DataUtil.executeStore("sp_ThanhVien_GetThanhVienDangNhap", param);
                 foreach (DataRow row in kq.Rows)
                 {
                     model = new ThanhVienModel();
                     model.MaThanhVien = row["MaThanhVien"].ToString();
                     model.TenDangNhap = row["TenDangNhap"].ToString();
                     model.TenVaiTro = row["TenVaiTro"].ToString();
                     model.MaVaiTro = row["MaVaiTro"].ToString();
                     model.NgayCapNhatCuoi = row["Ngay"].ToString();
                     model.TinhTrang = "1";

                 }
                 return model;
        }


        public static DataTable GetAllThanhVien1()
        {
            return DataUtil.executeStore("sp_ThanhVien_GetThanhVien", null);
        }


        /***/
        // hàm lấy tất cả thành viên
        /***/

        public static DataTable GetAllThanhVien()
        {
     
            SqlParameter[] param = new SqlParameter[2];
            int i = 0;
            param[i] = new SqlParameter("MaChiNhanh", SqlDbType.NVarChar);
            param[i++].Value = Constants.USER_LOGIN.MaChiNhanh ;
            param[i] = new SqlParameter("MaThanhVien", SqlDbType.NVarChar);
            param[i++].Value = Constants.USER_LOGIN.MaThanhVien;
            return DataUtil.executeStore("sp_ThanhVien_GetAllThanhVien", param);
           
        }

        /***/
        // cập nhật thông tin thành viên
        /***/

        public static Boolean UpdateInfo(ThanhVienModel model)
        {

            SqlParameter[] param = new SqlParameter[10];
            int i = 0;
            param[i] = new SqlParameter("MaThanhVien", SqlDbType.NVarChar);
            param[i++].Value = model.MaThanhVien;
            param[i] = new SqlParameter("HoTen", SqlDbType.NVarChar);
            param[i++].Value = model.HoTen;
            param[i] = new SqlParameter("DiaChi", SqlDbType.NVarChar);
            param[i++].Value = model.DiaChi;
            param[i] = new SqlParameter("DienThoai", SqlDbType.NVarChar);
            param[i++].Value = model.DienThoai;
            param[i] = new SqlParameter("Email", SqlDbType.NVarChar);
            param[i++].Value = model.Email;
            param[i] = new SqlParameter("BangCap", SqlDbType.NVarChar);
            param[i++].Value = model.Bangcap;
            param[i] = new SqlParameter("NgaySinh", SqlDbType.DateTime);
            param[i++].Value = model.NgaySinh;
            param[i] = new SqlParameter("TinhTrang", SqlDbType.NVarChar);
            param[i++].Value = model.TinhTrang;
            param[i] = new SqlParameter("MaVaiTro", SqlDbType.NVarChar);
            param[i++].Value = model.MaVaiTro;
            param[i] = new SqlParameter("MaChiNhanh", SqlDbType.NVarChar);
            param[i++].Value = model.MaChiNhanh;
            return DataUtil.executeNonStore("sp_ThanhVien_UpdateInfo", param);

        }


        public static Boolean Insert(ThanhVienModel model)
        {

            SqlParameter[] param = new SqlParameter[3];
            int i = 0;
            param[i] = new SqlParameter("username", SqlDbType.NVarChar);
            param[i++].Value = model.TenDangNhap;
            param[i] = new SqlParameter("password", SqlDbType.NVarChar);
            param[i++].Value = model.MatKhau;
            param[i] = new SqlParameter("role", SqlDbType.Int);
            param[i++].Value = model.MaVaiTro;
            
            return DataUtil.executeNonStore("sp_ThanhVien_Insert", param);

        }
    }
}
